﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Shl01.Dal;
using Shl01.Models;
using System.Data.SqlClient;

namespace Shl01
{
    public partial class SqlForm : Form
    {
        public SqlForm()
        {
            InitializeComponent();
        }
        public Conn Conn { get; set; }
        public string DataBase { get; set; }
        private void button1_Click(object sender, EventArgs e)
        {
            NewMethod();
        }

        private void NewMethod()
        {
            DataSet ds1 = null;
            try
            {
                ds1 = new Sql().GetTableDataSet(Conn, this.comboBox1.Text, DataBase);
                this.dataGridView1.DataSource = ds1.Tables[0];
                Quan("true");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SqlForm_Load(object sender, EventArgs e)
        {
            DataTable dt = new Fang().GetAllTables(Conn,DataBase);
            this.comboBox1.Items.Clear();
            foreach(DataRow dr in dt.Rows)
            {
                this.comboBox1.Items.Add(dr["TABLE_NAME"].ToString());
            }
            if(this.comboBox1.Items.Count > 0)
            {
                this.comboBox1.SelectedItem = this.comboBox1.Items[0];
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string sql = this.textBox2.Text + "[" + this.comboBox1.Text + "]" + this.textBox1.Text;
            Console.WriteLine(sql);
            try
            {
                DataSet ds = new Sql().GetDataSet(Conn,sql,DataBase);
                this.dataGridView1.DataSource = ds.Tables[0];
                Quan("true");
                MessageBox.Show("成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            string pan = "false";
            if(this.checkBox1.Checked == true)
            {
                pan = "true";
            }
            Quan(pan);
        }
        public void Quan(string pan)
        {
            foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
            {
                dgvr.Cells[0].Value = pan;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = this.comboBox1.Text + "Insert.sql";
            DialogResult result = save.ShowDialog();
            if(result == DialogResult.OK)
            {
                DataTable dt = this.dataGridView1.DataSource as DataTable;
                foreach(DataGridViewRow dgvr in this.dataGridView1.Rows)
                {
                    bool? pan = dgvr.Cells[0].Value as bool?;
                    if(pan == false)
                    {
                        dt.Rows.RemoveAt(Convert.ToInt32(dgvr.Index.ToString()));
                    }
                }
                try
                {
                    List<string> list = new Sql().SetDataSet(dt, this.comboBox1.Text, save.FileName);
                    MessageBox.Show("成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = this.comboBox1.Text + "Update.sql";
            DialogResult result = save.ShowDialog();
            if (result == DialogResult.OK)
            {
                DataTable dt = this.dataGridView1.DataSource as DataTable;
                foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                {
                    bool? pan = dgvr.Cells[0].Value as bool?;
                    if (pan == false)
                    {
                        dt.Rows.RemoveAt(Convert.ToInt32(dgvr.Index.ToString()));
                    }
                }
                try
                {
                    List<string> list = new Sql().SetUpdate(dt, this.comboBox1.Text, this.textBox3.Text, this.textBox4.Text, save.FileName);
                    MessageBox.Show("成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = this.comboBox1.Text + "Table.sql";
            DialogResult result = save.ShowDialog();
            if (result == DialogResult.OK)
            {
                DataTable dt = this.dataGridView1.DataSource as DataTable;
                try
                {
                    new Sql().SetTable(dt, save.FileName);
                    MessageBox.Show("成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = this.comboBox1.Text + "Database.sql";
            DialogResult result = save.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    new Sql().SetDatabase(DataBase, save.FileName);
                    MessageBox.Show("成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = DataBase + ".sql";
            DialogResult result = save.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    new Sql().SetData(DataBase,Conn, save.FileName);
                    MessageBox.Show("成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            DataTable dt = this.dataGridView1.DataSource as DataTable;
            try
            {
                BiaoShuoMIng.Update(dt,Conn,DataBase);
                MessageBox.Show("成功");
                NewMethod();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
